package algo;

import java.util.Iterator;

public class PeekingInterator_284 implements Iterator {
    boolean peeked = false;
    Iterator<Integer> iterator;
    int peekedValue;
    public PeekingInterator_284(Iterator<Integer> iterator) {
        // initialize any member here.
        this.iterator = iterator;

    }

    // Returns the next element in the iteration without advancing the iterator.
    public Integer peek() {
        if(peeked){
            return peekedValue;
        } else {
            if(iterator.hasNext()){
                peekedValue=iterator.next();
                peeked = true;
            }
        }
        return peekedValue;
    }

    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    @Override
    public Integer next() {
        if(peeked){
            peeked = false;
            return peekedValue;
        }

        return iterator.next();
    }

    @Override
    public boolean hasNext() {
        return peeked? true: iterator.hasNext();
    }

}
